# -*- coding: utf-8 -*-
"""
file:ch6_8意会定积分矩形切割法
author: Fuwen
time: 2022/7/20 7:16
software:PyCharm
"""
import numpy as np
import matplotlib.pyplot as plt

# 被积函数 f(x) = x**2,  x∈[-1,1]
# 被积函数系数
a = 1

# 绘制被积函数在积分区间内的图形区域
x = np.linspace(-1, 1, 1001)
y = a * x**2
plt.plot(x, y )
plt.fill_between(x, y1=y, y2=0, where=(x>=0)&(x<=1),facecolor='lightpink')

x_line = np.linspace(0, 0.1, 2)
y_line = x_line

# 绘制蓝色水平线
for i in range(40): # 分割40个区间
    x_line[0] = i*0.025
    x_line[1] = x_line[0] + 0.025
    y_line = [a*n**2 for n in x_line]
    y_line[1] = y_line[0]
    plt.plot(x_line, y_line, color='b')

# 绘制蓝色垂直线
for i in range(40):
    x_line[0] = i*0.025
    x_line[1] = x_line[0]
    y_line[0] = 0
    y_line[1] = a*x_line[0]**2
    plt.plot(x_line, y_line, color='b')

# 矩形切割后，s_sum = ∑∆x * y_left， 小的面积和
# 矩形切割后，L_sum = ∑∆x * y_right，大的面积和
# 整体区域面积A， s_sum < A < L_sum, A ≈ (s_sum + L_sum) / 2

plt.grid()
plt.show()